﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using System.Collections.Specialized;
using Sobiens.WPF.Controls.Classes;

namespace Sobiens.WPF.Controls.UserControls
{
    public delegate void PagingControlAfter_PageChange(int currentPageIndex, string listItemCollectionPositionNext);

    /// <summary>
    /// Interaction logic for PagingControl.xaml
    /// </summary>
    public partial class PagingControl : UserControl
    {
        private int CurrentPageIndex;
        private PageInfos ListItemCollectionPositionNexts = new PageInfos();

        public event PagingControlAfter_PageChange PreviousButtonClick;
        public event PagingControlAfter_PageChange NextButtonClick;
        public PagingControl()
        {
            InitializeComponent();
        }

        protected void PreviousButton_Click(object sender, RoutedEventArgs e)
        {
            CurrentPageIndex--;
            string currentListItemCollectionPositionNext = string.Empty;
            if (CurrentPageIndex > 0)
                currentListItemCollectionPositionNext = ListItemCollectionPositionNexts[CurrentPageIndex-1].ListItemCollectionPositionNexts;

            if (PreviousButtonClick != null)
                PreviousButtonClick(CurrentPageIndex, currentListItemCollectionPositionNext);
        }

        protected void NextButton_Click(object sender, RoutedEventArgs e)
        {
            string currentListItemCollectionPositionNext = ListItemCollectionPositionNexts[CurrentPageIndex].ListItemCollectionPositionNexts;
            CurrentPageIndex++;

            if (NextButtonClick != null)
                NextButtonClick(CurrentPageIndex, currentListItemCollectionPositionNext);
        }

        public void ChangePage(int currentPageIndex, string listItemCollectionPositionNext, int itemCount)
        {
            CurrentPageIndex = currentPageIndex;

            if (currentPageIndex == 0)
                ListItemCollectionPositionNexts = new PageInfos();

            if (ListItemCollectionPositionNexts[currentPageIndex] == null)
            {
                ListItemCollectionPositionNexts.Add(currentPageIndex, listItemCollectionPositionNext, itemCount);
            }

            NextButton.Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() =>
            {
                if (listItemCollectionPositionNext != String.Empty)
                {
                    NextButton.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    NextButton.Visibility = System.Windows.Visibility.Hidden;
                }
            }));

            PreviousButton.Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() =>
            {
                if (currentPageIndex > 0)
                {
                    PreviousButton.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    PreviousButton.Visibility = System.Windows.Visibility.Hidden;
                }
            }));
            
            PagingLabel.Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() =>
            {
                if (ListItemCollectionPositionNexts[currentPageIndex] != null)
                    PagingLabel.Content = ListItemCollectionPositionNexts[currentPageIndex].StartItemIndex.ToString() + " - " + ListItemCollectionPositionNexts[currentPageIndex].EndItemIndex.ToString();
                else
                    PagingLabel.Content = "0 - 0";
            }));

        }
    }
}
